package com.ylink.epcc.epccdemocustomization.handler;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import lombok.extern.slf4j.Slf4j;

import org.springframework.stereotype.Service;

import com.ylink.epcc.common.db.epcc201.entity.Epcc201DO;
import com.ylink.epcc.common.db.epcc201.repository.Epcc201Repository;
import com.ylink.epcc.common.db.epcc205.entity.Epcc205DO;
import com.ylink.epcc.common.db.epcc205.repository.Epcc205Repository;
import com.ylink.epcc.common.db.epcc209.entity.Epcc209DO;
import com.ylink.epcc.common.enums.RPFlgCdEnum;
import com.ylink.epcc.common.enums.RefundStateEnum;
import com.ylink.epcc.common.enums.SysRtnCdEnum;
import com.ylink.epcc.common.enums.TrxStatusCdEnum;
import com.ylink.epcc.common.utils.CommonUtil;
import com.ylink.epcc.common.utils.Xml2ObjUtil;
import com.ylink.epcc.common.xmldto.dto.Epcc209DTO;
import com.ylink.epcc.common.xmldto.dto.Epcc209MsgBody;
import com.ylink.epcc.common.xmldto.group.SysRtnInf;
import com.ylink.epcc.epccdemocustomization.service.EpccDemoCustomizationIService;

/**
 * 客户化示例
 * @author ouyangliang
 * epcc-demo-customization
 * 2017年7月28日 下午4:31:39
 */
@Slf4j
@Service
public class EpccDemoCustomizationHandler implements EpccDemoCustomizationIHandler {
	
	@Resource
	private Epcc205Repository epcc205Repository;
	
	@Resource
	private EpccDemoCustomizationIService epcc209Service;
	
	@Resource
	private Epcc201Repository epcc201Repository;
	
	private static final String TEMPLATE_303 = "303.ftl";
	private static final String XSD_209 = "com/ylink/epcc/epcc209/xsd/epcc.209.001.01.xsd";

	@Override
	public String handle(String msg) {
		return "";
	}
	
	public void doHandlerFillBefore(String msg){}
	
	
	public Epcc209DO doHandlerFill(String msg){
		List<String> v = Xml2ObjUtil.validateXML(msg, XSD_209);
		if (v == null || v.size() > 0) {
			return null;
		}
		Epcc209DTO epcc209DTO = Xml2ObjUtil.parseXml(Epcc209DTO.class, msg);
		if (null == epcc209DTO) {
	           return null;
	     }
		Epcc209DO epcc209DO = getEpcc209DO(epcc209DTO);
		return epcc209DO;
	}
	
	/**
	 * 
	 * @param epcc101DO
	 * @return
	 */
	public void  doHandlerBefore(Epcc209DO epcc209DO){}
	
	
	
	
	/**
	 * 方法描述: 报文处理
	 * @param epcc101DO
	 * @return
	 * String
	 */
	public String doHandler(Epcc209DO epcc209DO){
		
		Map<String, Object> map209 = new HashMap<String, Object>();
		map209.put("instgId", epcc209DO.getInstgId());
		map209.put("trxCtgy", epcc209DO.getTrxCtgy());
		map209.put("trxId", epcc209DO.getTrxId());
		boolean isDuplicate = epcc209Service.isDuplicate(map209); //判断报文是否重复
		if(isDuplicate){
			epcc209Service.update(epcc209DO);
		}else{
			epcc209Service.insert(epcc209DO);
		}
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("pyerAcctIssrId", epcc209DO.getInstgId());
		map.put("trxId", epcc209DO.getTrxId());
		map.put("rpFlg", RPFlgCdEnum.CD_1.getEnValue());
		Epcc205DO epcc205DO = epcc205Repository.select(map);
		if(null!=epcc205DO&&TrxStatusCdEnum.CD_00.getEnValue().equals(epcc205DO.getTrxStatus())){  //退款报文存在并且交易状态为成功
			
			map.put("trxId", epcc205DO.getOriTrxId());
			map.put("pyeeAcctIssrId", epcc205DO.getPyerAcctIssrId());
			map.put("rpFlg", RPFlgCdEnum.CD_2.getEnValue());
			Epcc201DO epcc201DO = epcc201Repository.getByPK(map);
			if(null!=epcc201DO&&TrxStatusCdEnum.CD_00.getEnValue().equals(epcc201DO.getTrxStatus())&&
					RefundStateEnum.PS_01.getEnValue().equals(epcc201DO.getRefundStatus())){  //协议支付交易状态成功且退款状态为退款成功
			}
			
			
		}
		return null;
	}
	
	public void doHandlerAfter(String result){}
	
	public Epcc209DO getEpcc209DO(Epcc209DTO epcc209DTO){
		
		Epcc209MsgBody body = epcc209DTO.getMsgBody();
		Epcc209DO epcc209DO = new Epcc209DO();
		return epcc209DO;
	}

}
